package com.oa.tools.jobs.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Node;

import com.oa.tools.TkMapper;
import com.oa.tools.entity.OAFlowInstanceWithBLOBs;
import com.oa.tools.entity.Person;
import com.oa.tools.entity.Stamp;
import com.oa.tools.jobs.FlowHandlerTaskBase;
import com.oa.tools.utils.XmlUtil;
import com.oa.tools.utils.XmlUtil.XMLHandler;

import tk.mybatis.mapper.entity.Example;

public class AddNewStampTask extends FlowHandlerTaskBase{

	public AddNewStampTask(Map<String,TkMapper<?>> daoMap) {
		super(daoMap);
		// TODO Auto-generated constructor stub
		
		this.flowStatus = FlowHandlerTaskBase.FLOW_STATUS_DONE;
		this.templateId = "15ec75886258347e137231346948c2d0";
	}

	@Override
	public String exec(boolean notice, OAFlowInstanceWithBLOBs obj) throws Exception {
		
		String compId = "fd_3597a2ca089bd4";
		String stampsId = "fd_35bae6beb5f072";
		String keeperId = "fd_35978ac0a977d2";
		String keeperDeptId = "fd_35978b7a45b66a";
		
		Map<String,String> stampsMap =new HashMap<>();
		stampsMap.put("1", "公章");
		stampsMap.put("2", "合同章");
		stampsMap.put("3", "法人章");
		stampsMap.put("4", "财务章");
		stampsMap.put("5", "发票专用章");
		stampsMap.put("6", "营业执照正本");
		stampsMap.put("7", "营业执照副本");
		
		StringBuilder sb = new StringBuilder();
		final Map<String,Object> resultMap = new HashMap<>();
		String xmlStr = obj.getExtendDataXml();
//		System.out.println(xmlStr);
		XmlUtil.parseXmlWithHandler(xmlStr, "/java/object/void",new XMLHandler() {
			
			@Override
			public void hanle(Node paramNode) throws Exception {
				Node ele1 = paramNode.selectSingleNode("string");
				
				if(ele1.getText().equals(compId)) {
					Node node = paramNode.selectSingleNode("string[2]");
					resultMap.put("company", node.getText());
				}else if(ele1.getText().equals(keeperId)) {
					Node node = paramNode.selectSingleNode("object/void/string[2]");
					resultMap.put("keeperId", node.getText());
					Node node1 = paramNode.selectSingleNode("object/void[2]/string[2]");
					resultMap.put("keeperName", node1.getText());
				}else if(ele1.getText().equals(keeperDeptId)) {
					Node node = paramNode.selectSingleNode("object/void/string[2]");
					resultMap.put("keeperDeptId", node.getText());
					Node node1 = paramNode.selectSingleNode("object/void[2]/string[2]");
					resultMap.put("deptName", node1.getText());
				}else if(ele1.getText().equals(stampsId)) {
					Node node = paramNode.selectSingleNode("string[2]");
					String arr[] = node.getText().split(";");
					List<String> stamps = new ArrayList<>();
					for(String stampId:arr) {
						stamps.add(stampsMap.get(stampId));
					}
					resultMap.put("stamps", stamps);
				}
				
			}
		});
		
		List<String> stamps = (List<String>) resultMap.get("stamps");
		String companyName =  resultMap.get("company").toString();
		String keeper_obj_id =  resultMap.get("keeperId").toString();
		String keeper_dept_obj_Id =  resultMap.get("keeperDeptId").toString();
		String keeperName =  resultMap.get("keeperName").toString();
		String deptName =  resultMap.get("deptName").toString();
		
		Person person = this.personDAO.selectByPrimaryKey(keeper_obj_id);
		String keeperLogname = person.getFdLoginName();
		
		for(String stampName:stamps) {
			Example exa_stamp = new Example(Stamp.class);
			exa_stamp.createCriteria().andEqualTo("company", companyName).andEqualTo("stampType", stampName);/*.andGreaterThan("docCreateTime", new Date(lastestDateMill))*/;
			List<Stamp> stampObjs = this.stampDAO.selectByExample(exa_stamp);
			if(stampObjs==null || stampObjs.size()==0) {
				Stamp stamp = new Stamp(companyName, stampName, deptName, keeperName, keeperLogname, (short)1);
				this.stampDAO.insert(stamp);
				sb.append("新增印章:["+companyName+"-"+stampName+"-"+deptName+"-"+keeperName+"-"+keeperLogname+"]\r\n");
			}
		}
		
		return sb.toString();
	}

	
}
